#include <iostream>
#include <string>
#include <fstream>
#include <boost/program_options.hpp>

#include "include/Configuration.h"

namespace po = boost::program_options;

ConfigDB Configuration::db;
ConfigMemcache Configuration::mmc;
ConfigGame Configuration::game;
string Configuration::config_file = "";

int Configuration::Init(char *config_file_name)
{
#ifdef _DEBUG_LEVEL_1_
	cout <<"Configuration::Init" << endl;
#endif

	config_file = config_file_name;

#ifdef _DEBUG_LEVEL_1_
	cout <<"Configuration::Init end, file name: " << config_file << endl;
#endif

	ifstream ifs(config_file.c_str());
	if (!ifs)
	{
		cout << "can not open config file: " << config_file << endl;
		return 1;
	}

	po::options_description config("Configuration");
	config.add_options()
		("db_name", po::value<string>(&db.name), "db_name")
		("db_server", po::value<string>(&db.server), "db_server")
		("db_user", po::value<string>(&db.user), "db_user")
		("db_passwd", po::value<string>(&db.passwd), "db_passwd")
		("db_port", po::value<int>(&db.port), "db_port")
		("mmc_server", po::value<string>(&mmc.server), "mmc_server")
		("mmc_port", po::value<int>(&mmc.port), "mmc_port")
		("mmc_db_port", po::value<int>(&mmc.db_port), "mmc_db_port")
		("protocol", po::value<int>(&game.protocol), "game_protocol")
		("version", po::value<int>(&game.version), "protocol_version")
		("game", po::value<int>(&game.game_id), "game_id")
		("server_type", po::value<int>(&game.server_type), "server_type")
		("server_id", po::value<int>(&game.server_id), "server_id")
		;

	po::options_description config_file_options;
	config_file_options.add(config);

	po::variables_map vm;
	store(parse_config_file(ifs, config_file_options), vm);
	notify(vm);

	if(vm.count("db_name"))
	{
		cout << "db_name: " << db.name << endl;
	}
	else
	{
		cout << "miss configuration: db_name" << endl;
		return 1;
	}

	if(vm.count("db_server"))
	{
		cout << "db_server: " << db.server << endl;
	}
	else
	{
		cout << "miss configuration: db_server" << endl;
		return 1;
	}

	if(vm.count("db_user"))
	{
		cout << "db_user: " << db.user << endl;
	}
	else
	{
		cout << "miss configuration: db_user" << endl;
		return 1;
	}

	if(vm.count("db_passwd"))
	{
		cout << "db_passwd: " << db.passwd << endl;
	}
	else
	{
		cout << "miss configuration: db_passwd" << endl;
		return 1;
	}

	if(vm.count("db_port"))
	{
		cout << "db_port: " << db.port << endl;
	}
	else
	{
		cout << "miss configuration: db_port" << endl;
		return 1;
	}

	if(vm.count("mmc_server"))
	{
		cout << "mmc_server: " << mmc.server << endl;
	}
	else
	{
		cout << "miss configuration: mmc_server" << endl;
		return 1;
	}

	if(vm.count("mmc_port"))
	{
		cout << "mmc_port: " << mmc.port << endl;
	}
	else
	{
		cout << "miss configuration: mmc_port" << endl;
		return 1;
	}
	
	if(vm.count("mmc_db_port"))
	{
		cout << "mmc_db_port: " << mmc.db_port << endl;
	}
	else
	{
		cout << "miss configuration: mmc_db_port" << endl;
		return 1;
	}

	if(vm.count("protocol"))
	{
		cout << "protocol: " << game.protocol << endl;
	}
	else
	{
		cout << "miss configuration: protocol" << endl;
		return 1;
	}

	if(vm.count("version"))
	{
		cout << "version: " << game.version << endl;
	}
	else
	{
		cout << "miss configuration: version" << endl;
		return 1;
	}

	if(vm.count("game"))
	{
		cout << "game_id: " << game.game_id << endl;
	}
	else
	{
		cout << "miss configuration: game_id" << endl;
		return 1;
	}

	if(vm.count("server_type"))
	{
		cout << "server_type: " << game.server_type << endl;
	}
	else
	{
		cout << "miss configuration: server_type" << endl;
		return 1;
	}

	if(vm.count("server_id"))
	{
		cout << "server_id: " << game.server_id << endl;
	}
	else
	{
		cout << "miss configuration: server_id" << endl;
		return 1;
	}

	return 0;
}

